<template>
  <div class="container-article">
    <!-- 筛选容器 -->
    <el-card>
      <div slot="header">
        <!-- 面包屑组件自定义 -->
        <my-bread>内容管理</my-bread>
      </div>
      <!-- 内容 -->
      <el-form label-width="80px" size="small">
        <el-form-item label="状态:">
          <!-- 状态单选框 -->
          <el-radio-group v-model="reqParams.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">已通过</el-radio>
            <el-radio :label="3">未通过</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道:">
          <!-- 频道下拉框,清空功能clearable -->
          <!-- <el-select
            @change="changeChannel"
            clearable
            v-model="reqParams.channel_id"
            placeholder="请选择"
          >
            <el-option
              v-for="item in channelOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>-->
          <my-channel :value="reqParams.channel_id" @input="reqParams.channel_id=$event"></my-channel>
        </el-form-item>
        <el-form-item label="日期:">
          <!-- 日期选择-数组[开始,结束] -->
          <el-date-picker
            v-model="dateArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeDate"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <!-- 绑定事件 -->
          <el-button type="primary" @click="filterArticle()">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 筛选结果容器 -->
    <el-card style="margin-top:20px">
      <div slot="header">筛选出 {{total}} 条结果:</div>
      <!-- 表格 -->
      <el-table :data="articles">
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]" class="cover-image">
              <div slot="error" class="image-slot">
                <img src="../../assets/error.gif" class="cover-image" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" width="400px"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status===0" type="primary">草稿</el-tag>
            <el-tag v-if="scope.row.status===1" type="info">待审核</el-tag>
            <el-tag v-if="scope.row.status===2" type="success">已通过</el-tag>
            <el-tag v-if="scope.row.status===3" type="warning">未通过</el-tag>
            <el-tag v-if="scope.row.status===4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="时间" prop="pubdate"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              @click="toEdit(scope.row.id)"
              type="primary"
              icon="el-icon-edit"
              circle
              plain
            ></el-button>
            <el-button
              @click="delArticle(scope.row.id)"
              type="danger"
              icon="el-icon-delete"
              circle
              plain
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页:上一页,页码,下一页 -->
      <el-pagination
        style="margin-top:20px"
        background
        layout="prev, pager, next"
        :page-size="reqParams.per_page"
        :total="total"
        :current-page="reqParams.page"
        @current-change="changePager"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "article-page",
  data() {
    return {
      // 筛选参数对象
      reqParams: {
        // 状态
        status: null,
        // 频道id
        channel_id: null,
        // 起始时间
        begin_pubdate: null,
        // 截至时间
        end_pubdate: null,
        // 当前页码
        page: 1,
        // 每页容量
        per_page: 20
      },
      // 日期
      dateArr: [],
      // 文章数据
      articles: [],
      // 下拉频道数据
      // channelOptions: [],
      // 总条数
      total: 0
    };
  },
  // 页面一加载就获取数据
  created() {
    // this.getChannels();
    this.getArticles();
  },
  methods: {
    // // 获取下拉列表数据
    // async getChannels() {
    //   const {
    //     data: { data }
    //   } = await this.$http.get("/channels");
    //   // console.log(res.data.data.channels);
    //   this.channelOptions = data.channels;
    // },
    // 获取文章列表
    async getArticles() {
      const {
        data: { data }
      } = await this.$http.get("/articles", { params: this.reqParams });
      // console.log(data);
      this.articles = data.results;
      this.total = data.total_count;
    },
    // 分页
    changePager(newPage) {
      this.reqParams.page = newPage;
      this.getArticles();
    },
    // 筛选事件
    filterArticle() {
      // 回到第一页
      this.reqParams.page = 1;
      this.getArticles();
    },
    // 日期选择
    changeDate(dateArr) {
      // 把选择好的时间赋值给开始/截止时间
      // dateArr是数组
      // 时间格式是 yyyy-MM-dd
      if (dateArr) {
        this.reqParams.begin_pubdate = dateArr[0];
        this.reqParams.end_pubdate = dateArr[1];
      } else {
        // 清空筛选条件
        this.reqParams.begin_pubdate = null;
        this.reqParams.end_pubdate = null;
      }
    },
    // // 频道选择
    // changeChannel() {
    //   // 不能传空字符串,可以改成null
    //   if (this.reqParams.channel_id === "") this.reqParams.channel_id = null;
    // },
    // 跳转编辑/publish?id=${id}
    toEdit(id) {
      this.$router.push({ path: "/publish", query: { id } });
    },
    // 删除
    delArticle(id) {
      this.$confirm("此操作将永久删除该文章, 是否继续?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            // 点确认发送删除请求
            await this.$http.delete("/articles/${id}");
            // 删除提示
            this.$message.success("删除成功");
            // 刷新页面
            this.getArticles();
          } catch (e) {
            // 删除失败
            this.$message.console.error("删除失败");
          }
        })
        .catch(() => {
          // 点取消什么也不用做
        });
    }
  }
};
</script>

<style lang="less" scoped>
.cover-image {
  width: 150px;
  height: 100px;
}
</style>